package com.pi.pro.xcmg.am.was.fac.calculator;


import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.pi.pro.xcmg.am.common.XcmgAmConstants;
import com.pi.wms.common.WmsConstants;
import com.pi.wms.dto.act.PutawayActionDto;
import com.pi.wms.dto.act.PutawayLineDto;
import com.pi.wms.entity.TwmBasLocation;
import com.pi.wms.entity.TwtActTaskList;
import com.pi.wms.entity.VwaInventoryPaLocationList;
import com.pi.wms.fac.LocationResolver;
import com.pi.wms.fac.PutawayCalculator;
import com.pi.wms.service.PaLocationListService;
import com.pi.wms.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 深浅库位上架规则
 */
@Slf4j
public class SlocationPaSeqPutawayCalculator implements PutawayCalculator {
    //
    protected String code= XcmgAmConstants.PA_CALCULATOR_NAME_SLOC_PA_SEQ_CALCULATOR;

    @Autowired
    private LocationResolver locationResolver;

    @Autowired
    private PaLocationListService paLocationListService;

    @Autowired
    private TaskService taskService;

    //
    private boolean movetransfercheck=true;
    private boolean quickTransferCheck = false;
    //
    private Map<Long, String> movetransferflags=new HashMap<>();

    private Multimap<Long, Long> locgroups= ArrayListMultimap.create();
    private Map<Long, Long> lastallocatetimes=new HashMap<>();

    @Override
    public String getCode() {
        return this.code;
    }

    @Override
    public TwmBasLocation calculate(String batchNumber, PutawayLineDto dataDto, PutawayActionDto actionDto) {
        log.debug("Calculator {} calculate start//{batchNumber:{}, container:{}}", this.code, batchNumber, dataDto.getContainNumber());
        TwmBasLocation paloc=null;
        if (actionDto==null){
            actionDto=new PutawayActionDto();
        }
        List<VwaInventoryPaLocationList> datas=null;
        actionDto.setCheckHeight(dataDto.getHeight());
        actionDto.setOrderby("");
        try{
            datas = paLocationListService.query(dataDto,actionDto);
            if ((datas==null)||(datas.size()==0)){
                return paloc;
            }
            for (VwaInventoryPaLocationList d:datas){
                if ((d==null) || (d.getId()==null)){
                    continue;
                }
                if (!WmsConstants.LOCATION_TYPE_BIN.equalsIgnoreCase(d.getType())){
                    continue;
                }
                paloc=locationResolver.find(d.getId());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return paloc;
    }

    @Override
    public List<TwmBasLocation> calculateBatch(String batchNumber, PutawayLineDto dataDto, PutawayActionDto actionDto) {
        return null;
    }

    //定时检测任务清单,
    //任务为空,则重新计算/校准
    private boolean reloadable(Long divisionid){
        boolean state=false;
        if (divisionid==null){
            return state;
        }
        Long lastime = this.lastallocatetimes.get(divisionid);
        if ((lastime==null) || (lastime<=0)){
            this.lastallocatetimes.put(divisionid, System.currentTimeMillis());
            return state;
        }
        if ((System.currentTimeMillis()-lastime)< (10*60*1000)){
            return state;
        }
        List<TwtActTaskList> tasks=null;
        try{
            tasks=this.taskService.queryWorkingTasks(divisionid, WmsConstants.TASK_TYPE_PUTAWAY);
            if ((tasks==null) || (tasks.size()==0)){
                state=true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return state;
    }

}
